﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fetco.entities;
using fetco.data;
using WebTime;

namespace fetco.service
{
    public class WebSessionService
    {
        public static bool AddSession(WebSession websession)
        {
            websession.Time = WebTime.WebTime.DateToLong();
            WebSessionDAO websession_dao = WebSessionDAO.GetInstance();
            websession_dao.SetSession(websession);
            if (websession.SessionId != 0)
                return true;
            return false;
        }

        public static bool CheckLogin(long user_id)
        {
            WebSessionDAO websession_dao = WebSessionDAO.GetInstance();
            long expire = 900;
            long time_curr = WebTime.WebTime.DateToLong();
            long time_expire = time_curr - expire;
            long time_login=websession_dao.GetTime(user_id);
            if(time_expire>time_login)
                return false;
            websession_dao.UpdateSession(user_id, time_curr);
            return true;
        }
        public static bool CheckLogin(string login_name)
        {
            WebSessionDAO websession_dao = WebSessionDAO.GetInstance();
            long expire = 900;
            long time_curr = WebTime.WebTime.DateToLong();
            long time_expire = time_curr - expire;
            long time_login = websession_dao.GetTime(login_name);
            if (time_expire > time_login)
                return false;
            websession_dao.UpdateSession(login_name, time_curr);
            return true;
        }

        public static bool RemoveSession()
        {
            long time = WebTime.WebTime.DateToLong();
            long expire = 900;
            long time_expire = time - expire;
            WebSessionDAO websession_dao = WebSessionDAO.GetInstance();
            return websession_dao.DelSession(time_expire);
        }
        public static bool RemoveSession(long user_id)
        {
            WebSessionDAO websession_dao = WebSessionDAO.GetInstance();
            return websession_dao.DelSessionBy(user_id);
        }
        public static bool RemoveSession(string login_name)
        {
            WebSessionDAO websession_dao = WebSessionDAO.GetInstance();
            return websession_dao.DelSessionBy(login_name);
        }
        
    }
}
